package com.handpay.nocardpay.baselibrary.main.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.handpay.nocardpay.baselibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyyoona7 on 2017/7/10.
 */

public class GestureLockDisplayView extends View {

    private static final String TAG = "GestureLockDisplayView";

    private Paint mPaint, mPaintLines;
    private Path mLinePath;

    private int mDotCount = 3;
    // n * mCircleRadius*2 + ( n + 1 ) * mDotMargin = getWidth();
    private int mCircleRadius;
    //mCircle*0.5
    private int mDotMargin;

    //选中颜色
    private int LINE_NORMAL_COLOR = 0xFF00ACF7;
    //未选中时颜色
    private int LINE_ERROR_COLOR = 0xFFFF2525;

    private List<Dot> mDotList = new ArrayList<>(1);
    private List<Integer> mAnswerList = new ArrayList<>(1);

    private boolean bSetAnswerRight = true;
    public GestureLockDisplayView(Context context) {
        this(context, null);
    }

    public GestureLockDisplayView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.BLACK);

        mPaintLines = new Paint();
        mPaintLines.setAntiAlias(true);
        mPaintLines.setStyle(Paint.Style.STROKE);
        mPaintLines.setStrokeWidth(5);
        mPaintLines.setColor(LINE_NORMAL_COLOR);

        mLinePath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        width = width > height ? height : width;
        setMeasuredDimension(width, width);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCircleRadius = (int) (getWidth() * 2 * 1.0f / (5 * mDotCount + 1));
        mDotMargin = (int) (0.8 * mCircleRadius);
        for (int i = 0; i < mDotCount * mDotCount; i++) {
            //计算圆心坐标
            float x = i % mDotCount * 2 * mCircleRadius + mCircleRadius + i % mDotCount * mDotMargin + mDotMargin;
            float y = i / mDotCount * 2 * mCircleRadius + mCircleRadius + i / mDotCount * mDotMargin + mDotMargin;
            //初始化点坐标
            mDotList.add(new Dot(x, y, i + 1));
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //同步dot状态
        syncAnswerState();
        if (bSetAnswerRight){
            mPaintLines.setColor(LINE_NORMAL_COLOR);
        }else {
            mPaintLines.setColor(LINE_ERROR_COLOR);
        }
        for (int i = 0; i < mAnswerList.size(); i++) {
            Dot dot = null;
            for (int j = 0; j < mDotList.size(); j++) {
                if (mDotList.get(j).getIndex() == mAnswerList.get(i)) {
                    dot = mDotList.get(j);
                }
            }
            float x = dot.getX();
            float y = dot.getY();
            if (i == 0) {
                mLinePath.moveTo(x, y);
            } else {
                mLinePath.lineTo(x, y);
            }
        }
        canvas.drawPath(mLinePath, mPaintLines);

        for (int i = 0; i < mDotList.size(); i++) {
            Dot dot = mDotList.get(i);
            Bitmap bitmap = null;
            if (dot.isSelected()) {
                if (bSetAnswerRight){
                    bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.gesture_lock_view_circle_selected_normal)).getBitmap();
                }else{
                    bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.gesture_lock_view_circle_selected_error)).getBitmap();
                }
            } else {
                bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.gesture_lock_view_gray_out_bg)).getBitmap();
            }
            bitmap = zoomImage(bitmap, mCircleRadius * 2, mCircleRadius * 2);
            canvas.drawBitmap(bitmap, dot.getX() - bitmap.getWidth() / 2, dot.getY() - bitmap.getHeight() / 2, mPaint);
        }
    }

    private void syncAnswerState() {
        //先重置所有点的状态
        for (Dot dot : mDotList) {
            dot.setSelected(false);
        }
        //设置答案index的状态
        for (int i = 0; i < mAnswerList.size(); i++) {
            int key = mAnswerList.get(i);
            for (int j = 0; j < mDotList.size(); j++) {
                if (mDotList.get(j).getIndex() == key) {
                    mDotList.get(j).setSelected(true);
                    continue;
                }
            }
        }
    }

    /**
     * 设置答案
     *
     * @param answer
     */
    public void setAnswer(boolean bRight, int... answer) {
        bSetAnswerRight = bRight;
        mAnswerList.clear();
        for (int i = 0; i < answer.length; i++) {
            mAnswerList.add(answer[i]);
        }
        postInvalidate();
    }


    public void setAnswer(boolean bRight,String answer) {
        bSetAnswerRight = bRight;
        char keys[] = answer.toCharArray();
        mAnswerList.clear();
        for (int i = 0; i < keys.length; i++) {

            mAnswerList.add(Character.getNumericValue(keys[i]));
        }
        postInvalidate();
    }

    public void clearAnswer() {
        mLinePath.reset();
        mAnswerList.clear();
        postInvalidate();
    }

    /**
     * 设置答案
     *
     * @param list
     */
    public void setAnswer(List<Integer> list) {
        this.mAnswerList = list;
        postInvalidate();
    }

//    /**
//     * 设置选中颜色
//     *
//     * @param color
//     */
//    public void setDotSelectedColor(int color) {
//        this.mDotSelectedColor = color;
//        postInvalidate();
//    }

//    /**
//     * 设置非选中颜色
//     *
//     * @param color
//     */
//    public void setDotUnSelectedColor(int color) {
//        this.mDotUnSelectedColor = color;
//        postInvalidate();
//    }

    /**
     * 设置点的个数
     *
     * @param dotCount
     */
    public void setDotCount(int dotCount) {
        this.mDotCount = dotCount;
    }


    public Bitmap zoomImage(Bitmap bgimage, double newWidth,
                            double newHeight) {
        // 获取这个图片的宽和高
        float width = bgimage.getWidth();
        float height = bgimage.getHeight();
        // 创建操作图片用的matrix对象
        Matrix matrix = new Matrix();
        // 计算宽高缩放率
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 缩放图片动作
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
                (int) height, matrix, true);
        return bitmap;
    }
}
